package com.ruoyi.api.user;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.api.user.requestVo.MachineBindRequestVo;
import com.ruoyi.api.user.requestVo.MessageListRequestVo;
import com.ruoyi.api.user.response.MessageResponseVo;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.user.domain.Message;
import com.ruoyi.user.domain.Warning;
import com.ruoyi.user.service.IMessageService;
import com.ruoyi.user.service.IWarningService;

import io.swagger.annotations.ApiOperation;

/**
 * 消息处理
 * @author ruoyi
 * @date 2018-12-23
 */
@Controller
@RequestMapping("/api/message")
public class ApiMessageController extends BaseController
{
    private String prefix = "message";
	
	@Autowired
	private IWarningService warningService;
	
	@Autowired
	private IMessageService messageService;
	
	/**
	 * 消息类型
	 */
	@PostMapping("/type")
	@ResponseBody
	@ApiOperation(value="消息类型", notes="消息列表",produces = "application/json")
	public HashMap<String, Object> type(@RequestBody MachineBindRequestVo bindRequestVo)
	{		
		HashMap<String, Object> parm = new HashMap<String, Object>();
		MessageResponseVo vo = new MessageResponseVo();
		/*vo.setContent("您好，您昨天使用了一键急救功能，并…您好，您昨天使用了一键急救功能，并…您好，您昨天使用了一键急救");
		vo.setCreateTime("2019-1-6");
		parm.put("healthMessage", vo);
		parm.put("dataMessage", vo);
		Warning param = new Warning();
		param.setImei(bindRequestVo.getCode());
		param.setWarningType(1);
		List<Warning> list = warningService.selectWarningList(param);
		if(list!=null&&list.size()>0){
			Warning warn = list.get(0);
			MessageResponseVo vo1 = new MessageResponseVo();
			vo1.setContent("您好，您的设备【"+bindRequestVo.getCode()+"】在"+warn.getAddress()+"发生SOS告警，请关注");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			vo1.setCreateTime(sdf.format(warn.getCreateTime()));
			parm.put("sosMessage", vo1);
		}else{
			vo.setContent("暂无数据");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			vo.setCreateTime(sdf.format(new Date()));
			parm.put("sosMessage", vo);
		}*/
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//1、异常数据
		Message paramMsg = new Message();
		paramMsg.setImei(bindRequestVo.getCode());
		paramMsg.setType(1);
		List<Message> dataList = messageService.selectMessageList(paramMsg);
		if(dataList!=null&&dataList.size()>0){
			Message msg = dataList.get(0);
			MessageResponseVo dataMessage = new MessageResponseVo();
			dataMessage.setContent(msg.getContent());
			dataMessage.setCreateTime(sdf.format(msg.getCreateTime()));
			parm.put("dataMessage", dataMessage);
		}else{
			vo.setContent("暂无数据");
			vo.setCreateTime(sdf.format(new Date()));
			parm.put("dataMessage", vo);
		}
		//2、SOS数据
		paramMsg.setType(2);
		List<Message> sosList = messageService.selectMessageList(paramMsg);
		if(sosList!=null&&sosList.size()>0){
			Message msg = sosList.get(0);
			MessageResponseVo sosMessage = new MessageResponseVo();
			sosMessage.setContent(msg.getContent());
			sosMessage.setCreateTime(sdf.format(msg.getCreateTime()));
			parm.put("sosMessage", sosMessage);
		}else{
			vo.setContent("暂无数据");
			vo.setCreateTime(sdf.format(new Date()));
			parm.put("sosMessage", vo);
		}
		//3、健康报告
		vo.setContent("暂无数据");
		vo.setCreateTime(sdf.format(new Date()));
		parm.put("healthMessage", vo);
		
		return successApp(parm);
	}
	
	/**
	 * 消息列表
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value="消息列表", notes="消息列表",produces = "application/json")
	public HashMap<String, Object> list(@RequestBody MessageListRequestVo listRequestVo)
	{		
		List<MessageResponseVo> listVo = new ArrayList<MessageResponseVo>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Message paramMsg = new Message();
		paramMsg.setImei(listRequestVo.getCode());
		paramMsg.setType(listRequestVo.getType());
		List<Message> dataList = messageService.selectMessageList(paramMsg);
		if(dataList!=null&&dataList.size()>0){
			for(Message msg:dataList){
				MessageResponseVo vo = new MessageResponseVo();
				vo.setContent(msg.getContent());
				vo.setCreateTime(sdf.format(msg.getCreateTime()));
				listVo.add(vo);
			}
		}else{
			MessageResponseVo vo = new MessageResponseVo();
			vo.setContent("暂无数据");
			vo.setCreateTime(sdf.format(new Date()));
			listVo.add(vo);
		}
		return successApp(listVo);
	}

}
